home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Simple Help / TestHelp.c < prev   
C/C++ Source or Header  |  1994-10-13  |  13KB  |  590 lines

  1. /************************************************************************************************
  2. *
  3. *
  4. *        ColourLabMain.c        - Main program for ColourLab application
  5. *
  6. *
  7. *        15/7/94        ©1994 by Graham Cox
  8. *
  9. *
  10. *************************************************************************************************/
  11.  
  12. #include    "ColourLabHeaders.h"
  13.  
  14. Boolean            gDone = FALSE;
  15. short            gSleepTime;
  16. WindowPtr        gColourPalette;
  17. WindowMenuHdl    gPalettesList = NULL;
  18. OSType            gCreatorType = 'gCox';
  19. SimHelpHdl        gHelp = NULL;
  20. WindowPtr        gCalWindow = NULL;
  21. WindowPtr        gCustomToolPalette = NULL;
  22. Boolean            gIsInBackground = FALSE;
  23. EventRecord        gCurrentEvent;
  24. wVisStatusHdl    gFloaterStatus = NULL;
  25.  
  26. void    main(void)
  27. {
  28.     EventRecord            theEvent;
  29.     short                wneLoopCount;
  30.     
  31.     StdMacInit(8);        // standard initialisation code
  32.     ColourLabInit();    // init this app
  33.     
  34.     // for power PC, we should fetch events less frequently for better performance. This ensures that
  35.     
  36.     wneLoopCount = kWNEFetchDuty;
  37.  
  38.     while (! gDone)
  39.     {
  40.         if (wneLoopCount >= kWNEFetchDuty)
  41.         {
  42.             wneLoopCount = 0;
  43.             if (WaitNextEvent(everyEvent,&theEvent,gSleepTime,NULL))
  44.                 HandleOneEvent(&theEvent);
  45.             else
  46.                 SSRun(gSlideShow);
  47.         }
  48.         else
  49.         {
  50.             wneLoopCount++;
  51.             // this is where idle tasks, etc should be inserted
  52.             StopCursorAnimation();
  53.             AdjustImageCursor(TopDocument());
  54.             AnimateSelection(TopDocument());
  55.         }
  56.     }
  57.     VideoCleanUp();
  58.     //CleanUpGXPrinting();
  59. }
  60.  
  61.  
  62. void    HandleOneEvent(EventRecord *theEvent)
  63. {
  64.     WindowPtr    targWindow;
  65.     char        keyChar;
  66.     long        mSelect;
  67.     OSErr        theErr;
  68.     MenuHandle    sampleMenu;
  69.     
  70.     gCurrentEvent = *theEvent;
  71.     
  72.     switch (theEvent->what)
  73.     {
  74.         case mouseDown:
  75.             HandleMouseEvent(theEvent);
  76.             break;
  77.         case updateEvt:
  78.             targWindow = (WindowPtr) theEvent->message;
  79.             
  80.             SetPort(targWindow);
  81.             BeginUpdate(targWindow);
  82.             // draw something
  83.             
  84.             if (Is3DKind(targWindow))
  85.                 DrawTypicalWindow(targWindow);
  86.                 
  87.             if (IsColoursPalette(targWindow))
  88.                 DrawColoursPalette(targWindow);
  89.                 
  90.             if (IsCaliperWindow(targWindow))
  91.                 DrawCaliperWindow(targWindow);
  92.  
  93.             if (IsToolWindow(targWindow))
  94.                 UpdateTools(targWindow);
  95.                 
  96.             if (targWindow == gSlideShowPalette)
  97.                 UpdateSlideInfo(gSlideShow);
  98.                     
  99.             EndUpdate(targWindow);
  100.             break;
  101.         case keyDown:
  102.             keyChar = theEvent->message & charCodeMask;
  103.             if ((theEvent->modifiers & cmdKey) == cmdKey)
  104.             {
  105.                 AdjustMenus();
  106.                 sampleMenu = GetMHandle(kImageMenu);
  107.                 mSelect = PowerMenuKey(theEvent->message,theEvent->modifiers,sampleMenu);
  108.                 HandleMenuSelection(mSelect);
  109.             }
  110.             else
  111.                 HandleKeypress(TopDocument(),keyChar);
  112.             break;
  113.         case autoKey:
  114.             keyChar = theEvent->message & charCodeMask;
  115.             HandleKeypress(TopDocument(),keyChar);
  116.             break;
  117.         case activateEvt:
  118.             targWindow = (WindowPtr) theEvent->message;
  119.             SetPort(targWindow);
  120.             Activate3DWindow(targWindow,theEvent->modifiers & activeFlag);
  121.             break;
  122.         case diskEvt:
  123.             // don't bother to handle this one- SF or the Finder will get it eventually
  124.             break;
  125.         case osEvt:
  126.             HandleOSEvent(theEvent);
  127.             break;
  128.         case kHighLevelEvent:
  129.             theErr = AEProcessAppleEvent(theEvent);
  130.             break;
  131.     }
  132. }
  133.  
  134.  
  135. SimHelpHdl    OpenHelp()
  136. {
  137.     SimHelpHdl    sHelp;
  138.     DialogPtr    hDlog;
  139.     short        itemType;
  140.     Handle        itemHand;
  141.     Rect        itemBox;
  142.     GrafPtr        savePort;
  143.     static UserItemUPP    my3DItem;
  144.     
  145.     sHelp = GetNewHelpDialog(kHelpDialogID,kListUserItem,kTextUserItem,kTopicResID);
  146.     
  147.     if (sHelp != NIL)
  148.     {
  149.         hDlog = GetHelpDialogHdl(sHelp);
  150.         HideDItem(hDlog,ok);
  151.         
  152.         InstallColourUserItem(hDlog,kColourPopUpItem);
  153.         GetDItem(hDlog,k3DAdornmentItem,&itemType,&itemHand,&itemBox);
  154.         if ((itemType & 0x7F) == userItem)
  155.         {
  156.             my3DItem = NewUserItemProc((ProcPtr) ThreeDDrawProc);
  157.             itemHand = (Handle) my3DItem;
  158.             SetDItem(hDlog,k3DAdornmentItem,itemType,itemHand,&itemBox);
  159.             GetDItem(hDlog,k3DAdornmentItem - 1,&itemType,&itemHand,&itemBox);
  160.             itemHand = (Handle) my3DItem;
  161.             SetDItem(hDlog,k3DAdornmentItem - 1,itemType,itemHand,&itemBox);
  162.         }
  163.         RegisterNewWindow(gWMList,(WindowPtr) hDlog);
  164.         
  165.         ShowFWWindow(hDlog);
  166.         SelectFWWindow(hDlog);
  167.     }
  168.     return(sHelp);
  169. }
  170.  
  171.  
  172. #ifndef __CURSORUTILITIES__
  173. void    SetWatchCursor(void)
  174. {
  175.     CursHandle    watch;
  176.     
  177.     watch = GetCursor(watchCursor);
  178.     SetCursor(*watch);
  179. }
  180. #endif
  181.  
  182. void    HandleMouseEvent(EventRecord *theEvent)
  183. {
  184.     /* This is called when the event retrieved was a mouseDown */
  185.     
  186.     short        targLoc,newJawWidth;
  187.     WindowPtr    targWindow;
  188.     Rect        dragBounds,growBounds;
  189.     long        mSelect,growFactor;
  190.     RGBColor    pickColour;
  191.     
  192.     CalcIdealDocumentSizeUPP    theCalcProc;
  193.  
  194.     targLoc = FindWindow(theEvent->where,&targWindow);
  195.     dragBounds = (*GetGrayRgn())->rgnBBox;
  196.     InsetRect(&dragBounds,4,4);
  197.     
  198.     switch (targLoc)
  199.     {
  200.         case inMenuBar:
  201.             CheckWindowInMenu(gWMList,TopDocument());
  202.             CheckWindowInMenu(gPalettesList,FirstVisibleFloater());
  203.             AdjustMenus();
  204.             mSelect = MenuSelect(theEvent->where);
  205.             HandleMenuSelection(mSelect);
  206.             break;
  207.         case inDrag:
  208.             DragFWWindow(targWindow,theEvent->where,&dragBounds);
  209.             break;
  210.         case inGrow:
  211.             if (! IsFloating(targWindow))
  212.             {
  213.                 SetRect(&growBounds,kMinWindowWidth,kMinWindowHeight,dragBounds.right,dragBounds.bottom);
  214.                 growFactor = GrowWindow(targWindow,theEvent->where,&growBounds);
  215.                 if (growFactor)
  216.                 {
  217.                     SetPort(targWindow);
  218.                     if (Is3DKind(targWindow))
  219.                         Resize3DWindow(targWindow,LoWord(growFactor),HiWord(growFactor),FALSE);
  220.                     else
  221.                         SizeWindow(targWindow,LoWord(growFactor),HiWord(growFactor),FALSE);
  222.                 }
  223.             }
  224.             else
  225.             {
  226.                 if (IsToolWindow(targWindow))
  227.                 {
  228.                     mSelect = ClickInToolWindow(targWindow,theEvent);
  229.                     SelectTool(TopDocument(),mSelect);
  230.                 }
  231.             }
  232.             break;
  233.         case inGoAway:
  234.             if(TrackGoAway(targWindow,theEvent->where))
  235.             {
  236.                 CloseTheWindow(targWindow);
  237.                 CalcWindowPointers();
  238.             }
  239.             break;
  240.         case inZoomIn:
  241.         case inZoomOut:
  242.             if(TrackBox(targWindow,theEvent->where,targLoc))
  243.             {
  244.                 theCalcProc = NewCalcIdealDocumentSizeProc(CalcOptimumZoomSize);
  245.                 ZoomTheWindow((WindowPeek) targWindow,targLoc,theCalcProc);
  246.                 DisposeRoutineDescriptor(theCalcProc);
  247.             }
  248.             break;
  249.         case inSysWindow:
  250.             SystemClick(theEvent,targWindow);
  251.             break;
  252.         case inContent:
  253.             if (targWindow != TopDocument())
  254.             {
  255.                 SelectFWWindow(targWindow);
  256.                 if (IsToolWindow(targWindow))
  257.                 {
  258.                     mSelect = ClickInToolWindow(targWindow,theEvent);
  259.                     SelectTool(TopDocument(),mSelect);
  260.                 }
  261.                 
  262.                 if (IsColoursPalette(targWindow))
  263.                 {
  264.                     FindColourClicked(targWindow,theEvent->where,&pickColour);
  265.                     if (theEvent->modifiers & optionKey)
  266.                         (*gPaintInfo)->lineColour = pickColour;
  267.                     else
  268.                         (*gPaintInfo)->fillColour = pickColour;
  269.                 }
  270.             }
  271.             else
  272.                 // handle mouse clicks in the window here
  273.             
  274.                 Click3DWindow(targWindow,theEvent->where,theEvent->modifiers);
  275.             break;
  276.         case wInCaliperThumb + 2:
  277.             // this must be the caliper window- no other WDEF could return this value
  278.             newJawWidth = DragCaliperThumb(targWindow,theEvent->where);
  279.             SetNewJawWidth(targWindow,newJawWidth);
  280.             break;
  281.         default:
  282.             SysBeep(1);
  283.             break;
  284.     }
  285. }
  286.  
  287.  
  288. void    TranslateGXMenuItem(short menuID,short *itemID)
  289. {
  290.     // if GX isn't present, this ensures that the menu item is correct for the menu dispatcher even if
  291.     // the menu has been modified
  292.  
  293.     if (menuID == kFileMenu && ! gGxIsPresent)
  294.     {
  295.         if (*itemID == kItemCustomPageSetup)
  296.             *itemID = kItemPrint;
  297.         else
  298.             if (*itemID == kItemPrintOne)
  299.                 *itemID = kItemQuit;
  300.     }
  301. }
  302.  
  303.  
  304.  
  305. #define    kAboutBoxAlertID    256
  306.  
  307.  
  308. void    DisplayAboutBox(void)
  309. {
  310.     /* Display an alert which gives some info about this app */
  311.     
  312.     short    aHit;
  313.     
  314.     ParamText("\pJohn Lennon","\pThe Beatles",NULL,NULL);
  315.     
  316.     aHit = Alert(kAboutBoxAlertID,NULL);
  317. }
  318.  
  319.  
  320. pascal void    ThreeDDrawProc(WindowPtr theWindow,short theItem)
  321. {
  322.     /* This drawproc implements some non-vital 3D shading effects for a useritem */
  323.     
  324.     short        itemType;
  325.     Handle        itemHand;
  326.     Rect        itemBox;
  327.     
  328.     GetDItem(theWindow,theItem,&itemType,&itemHand,&itemBox);
  329.     Frame3DRect(&itemBox,FALSE);
  330.     
  331.     // Also create 3D effect for the window as a whole
  332.     
  333.     itemBox = qd.thePort->portRect;
  334.     InsetRect(&itemBox,1,1);
  335.     Frame3DRect(&itemBox,TRUE);
  336. }
  337.  
  338.  
  339. void    SetEditableHelp(SimHelpHdl theHelp,Boolean isEditable)
  340. {
  341.     TDRecHdl    tPanelHdl;
  342.     DialogPtr    hDlog;
  343.     
  344.     if(theHelp)
  345.     {
  346.         hDlog = GetHelpDialogHdl(theHelp);
  347.         
  348.         tPanelHdl = (TDRecHdl) GetWRefCon(hDlog);
  349.         
  350.         if (tPanelHdl)
  351.             (*tPanelHdl)->editable = isEditable;
  352.             
  353.         if (isEditable)
  354.         {
  355.             TEAutoView(TRUE,(*tPanelHdl)->TDText);
  356.             TEActivate((*tPanelHdl)->TDText);
  357.         }
  358.         else
  359.         {
  360.             TEDeactivate((*tPanelHdl)->TDText);
  361.             TEAutoView(FALSE,(*tPanelHdl)->TDText);
  362.         }
  363.     }
  364. }
  365.  
  366.  
  367. void    CloseTheWindow(WindowPtr theWindow)
  368. {
  369.     // closes the window. Action depends on type of window passed. If a document, the image status is
  370.     // checked and confirmed with user if save is needed
  371.     
  372.     short        iClose;
  373.     OSErr        theErr;
  374.     GrafPtr        savePort;
  375.     
  376.     if (theWindow)
  377.     {
  378.         if (Is3DKind(theWindow))
  379.         {
  380.             iClose = ImageShouldBeSaved(theWindow,gDone);
  381.             
  382.             switch (iClose)
  383.             {
  384.                 default:
  385.                 case kSaveTheImage:
  386.                     theErr = SavePictureFile(theWindow,FALSE);    
  387.                     if (theErr == noErr)
  388.                     {
  389.                         UnregisterWindow(gWMList,theWindow);
  390.                         Dispose3DWindow(theWindow);
  391.                         ClearUndoBuffer(gUndoInfo);
  392.                     }
  393.                     else
  394.                         SysBeep(1);
  395.                     break;
  396.                 case kDontSaveTheImage:
  397.                     UnregisterWindow(gWMList,theWindow);
  398.                     Dispose3DWindow(theWindow);
  399.                     ClearUndoBuffer(gUndoInfo);
  400.                     break;
  401.                 case kCancelTheOperation:
  402.                     if (gDone)
  403.                         gDone = FALSE;
  404.                     break;
  405.             }
  406.             if (gColourPalette)
  407.             {
  408.                 GetPort(&savePort);
  409.                 SetPort(gColourPalette);
  410.                 InvalRect(&qd.thePort->portRect);
  411.                 SetPort(savePort);
  412.             }
  413.             if (gSlideShow)
  414.             {
  415.                 if ((*gSlideShow)->owner == theWindow)
  416.                     (*gSlideShow)->owner = NULL;
  417.             
  418.             }
  419.         }
  420.         else
  421.         {
  422.             if (! IsFloating(theWindow))
  423.                 UnregisterWindow(gWMList,theWindow);
  424.                 
  425.             HideFWWindow(theWindow);
  426.         }
  427.         // recompute the floating stuff and send the right activate events
  428.         
  429.         CalcWindowPointers();
  430.         ActivateDocuments(TopDocument(),NULL);
  431.     }
  432. }
  433.  
  434.  
  435. void    CloseAllWindows()
  436. {
  437.     WindowPeek    temp,next;
  438.     
  439.     temp = (WindowPeek) FrontWindow();
  440.     
  441.     while(temp)
  442.     {
  443.         next = temp->nextWindow;
  444.         CloseTheWindow((WindowPtr) temp);
  445.         temp = next;
  446.     }
  447. }
  448.  
  449.  
  450. pascal OSErr    OpenAppAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  451. {
  452.     // apple event handler for the OpenApplication apple event message
  453.     
  454.     HiliteMenu(kFileMenu);
  455.     HandleMenuSelection(((long) kFileMenu << 16) + kItemNew);
  456.     return(noErr);
  457. }
  458.  
  459.  
  460. pascal OSErr    OpenDocsAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  461. {
  462.     // apple event handler for the OpenDocuments apple event message. This extracts the document aliases
  463.     // from the event and opens the files passed
  464.     
  465.     OSErr        theErr;
  466.     FSSpec        theFile;
  467.     long        docCount,index;
  468.     Size        actualSize;
  469.     AEDescList    docList;
  470.     AEKeyword    keyWord;
  471.     DescType    returnedType;
  472.     
  473.     theErr = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
  474.     
  475.     if (theErr == noErr)
  476.     {
  477.         theErr = AECountItems(&docList,&docCount);
  478.         
  479.         if (! theErr)
  480.         {
  481.             for (index = 0;index < docCount;index++)
  482.             {
  483.                 theErr = AEGetNthPtr(    &docList,
  484.                                         index +1,
  485.                                         typeFSS,
  486.                                         &keyWord,
  487.                                         &returnedType,
  488.                                         &theFile,
  489.                                         sizeof(FSSpec),
  490.                                         &actualSize);
  491.                 if (! theErr)
  492.                 {
  493.                     // open the file that we have been passed. We pass the buck on to another
  494.                     // function, which checks the filetype, etc
  495.                     
  496.                     HiliteMenu(kFileMenu);
  497.                     (void) OpenPicFileFromSpec(&theFile,NULL);    
  498.                     HiliteMenu(0);
  499.                 }
  500.             
  501.             }
  502.         }
  503.         theErr = AEDisposeDesc(&docList);
  504.     }
  505.     return(theErr);
  506. }
  507.  
  508.  
  509. pascal OSErr    PrintDocsAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  510. {
  511.     // apple event handler for the PrintDocuments apple event message (TO DO)
  512.     
  513.     return(noErr);
  514. }
  515.  
  516.  
  517. pascal OSErr    QuitAppAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  518. {
  519.     // apple event handler for the QuitApplication apple event message
  520.     
  521.     gDone = TRUE;
  522.     RecordPalettePositions();
  523.     CloseAllWindows();
  524.     return(noErr);
  525. }
  526.  
  527.  
  528. void    HandleOSEvent(EventRecord *theEvent)
  529. {
  530.     // handles and dispatches os Events such as suspend and resume
  531.  
  532.     Boolean     suspend;
  533.     
  534.     suspend = !(theEvent->message & 1);
  535.     
  536.     if (suspend)
  537.     {
  538.         HLVideoControl(kVideoPaused,gVideoChannel,TopDocument());
  539.  
  540.         // pause the slide show if it is running.
  541.         
  542.         if (gSlideShow)
  543.         {
  544.             if ((*gSlideShow)->ssStatus == kSSRunning)
  545.             {
  546.                 SlideShowControl(gSlideShow,kSSPauseShow);
  547.                 SSRun(gSlideShow);    // force it to stop now
  548.             }
  549.         }
  550.         gIsInBackground = TRUE;
  551.         gSleepTime = kBkgndSleepTime;
  552.         
  553.         // we should hide all of our floating windows and clipboard. We need to
  554.         // record the viz status of the windows so that we can restore them later
  555.         
  556.         gFloaterStatus = RecordWindowVisStatus();
  557.         ShowHideFloaters(kHideAllFloaters);
  558.     }
  559.     else
  560.     {
  561.         gIsInBackground = FALSE;
  562.         gSleepTime = kSleepTime;
  563.         
  564.         // we should show the floating windows that were showing before
  565.         
  566.         RestoreWindowVisStatus(gFloaterStatus);
  567.         gFloaterStatus = NULL;
  568.         
  569.         HLVideoControl(kVideoPaused,gVideoChannel,TopDocument());
  570.     }
  571. }
  572.  
  573.  
  574. void    RecordPalettePositions()
  575. {
  576.     // saves all the palettes in the prefs file so that they reappear where the user put them last
  577.     
  578.     short    index = kPalPrefBaseID;
  579.  
  580.     SaveWindowLocation(gColourPalette,index++,TRUE,FALSE);
  581.     SaveWindowLocation(gCalWindow,index++,TRUE,FALSE);
  582.     SaveWindowLocation(gCustomToolPalette,index++,TRUE,FALSE);
  583.     
  584.     SaveWindowLocation(gSlideShowPalette,kSSWindowLocPrefID,TRUE,FALSE);
  585. }
  586.  
  587.  
  588. // Mein Hund hat kein Nase!
  589. // Kein Nase? Wie riecht er?
  590. // Schrecklich!!!